﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ButtonTree2
{
    public partial class ButtonTree : UserControl
    {
        public ButtonTree()
        {
            InitializeComponent();
            contextMenuStrip = new ContextMenuStrip();
            contextMenuStrip.Renderer = MenuItemRanderer.Instance;
            BindMenu();
        }
        #region 属性

        public ButtonTree parent { get; set; }
        public List<ButtonTree> children { get; set; }
        public Boolean isClickable { get; set; }
        public ContextMenuStrip contextMenuStrip
        {
            get;
            set;
        }
        #endregion
        private ToolStripMenuItem GetMenuItem(string txt, Image img)
        {
            ToolStripMenuItem menuItem = new ToolStripMenuItem();
            menuItem.Text = txt;
            menuItem.Image = img;
            return menuItem;
        }
        private void BindMenu()
        {
            ToolStripMenuItem menu0 = GetMenuItem("一级1", null);
            ToolStripMenuItem menu01 = GetMenuItem("二级11", null);
            ToolStripMenuItem menu02 = GetMenuItem("二级12", null);
            menu0.DropDownItems.Add(menu01);
            menu0.DropDownItems.Add(menu02);
            contextMenuStrip.Items.Add(menu0);
            ToolStripMenuItem menu1 = GetMenuItem("一级2", null);
            ToolStripMenuItem menu11 = GetMenuItem("二级21", null);
            ToolStripMenuItem menu12 = GetMenuItem("二级22",null);
            menu1.DropDownItems.Add(menu11);
            menu1.DropDownItems.Add(menu12);
            contextMenuStrip.Items.Add(menu1);
        }
        private void button1_Click(object sender, EventArgs e)
        {
            Button btn = sender as Button;
            btn.FlatAppearance.BorderSize = 0;
            Point point = new Point(btn.Left + this.Left + 5, btn.Top + this.Top + 40);
            contextMenuStrip.Show(point);
        }
    }
}
